package com.dms.modules.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.modules.merchant.entity.Merchant;
import com.dms.modules.product.entity.Product;
import com.dms.modules.user.entity.UserFavorite;
import com.dms.modules.user.service.UserFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/user/favorite")
@Api(tags = "用户收藏接口")
@Validated
@PreAuthorize("hasRole('USER')")
public class UserFavoriteController {

    @Autowired
    private UserFavoriteService userFavoriteService;

    @PostMapping("/add")
    @ApiOperation("添加收藏")
    public ApiResponse<Boolean> addFavorite(@RequestParam @NotNull(message = "目标ID不能为空") Long targetId,
                                          @RequestParam @NotNull(message = "收藏类型不能为空") Integer type) {
        return ApiResponse.success(userFavoriteService.addFavorite(type, targetId));
    }

    @DeleteMapping("/cancel")
    @ApiOperation("取消收藏")
    public ApiResponse<Boolean> cancelFavorite(@RequestParam @NotNull(message = "目标ID不能为空") Long targetId,
                                             @RequestParam @NotNull(message = "收藏类型不能为空") Integer type) {
        return ApiResponse.success(userFavoriteService.removeFavorite(type, targetId));
    }

    @GetMapping("/check")
    @ApiOperation("检查是否已收藏")
    public ApiResponse<Boolean> isFavorited(@RequestParam @NotNull(message = "目标ID不能为空") Long targetId,
                                          @RequestParam @NotNull(message = "收藏类型不能为空") Integer type) {
        return ApiResponse.success(userFavoriteService.isFavorite(type, targetId));
    }

    @GetMapping("/count")
    @ApiOperation("获取收藏数量")
    public ApiResponse<Long> getFavoriteCount(@RequestParam @NotNull(message = "目标ID不能为空") Long targetId,
                                            @RequestParam @NotNull(message = "收藏类型不能为空") Integer type) {
        return ApiResponse.success(userFavoriteService.getFavoriteCount(targetId, type));
    }

    @GetMapping("/list")
    @ApiOperation("获取收藏列表")
    public ApiResponse<List<UserFavorite>> getFavoriteList(@RequestParam(required = false) Integer type,
                                                         @RequestParam(defaultValue = "10") @Min(value = 1, message = "限制数量不能小于1") Integer limit) {
        return ApiResponse.success(userFavoriteService.getByUserId(type, limit));
    }
} 